/** The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License.
 *
 * The Original Code is Fluid-Cubed (at http://www.adamldavis.com/crystal/fluid.html).
 *
 * The Initial Developer of the Original Code is Adam L. Davis. 
 * Portions created by Adam L. Davis are Copyright (C) 2007 Adam L. Davis. All Rights Reserved.
 *
 * Contributor(s): Adam L. Davis.
 */
package tractorlk;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.Timer;

import tractorlk.drops.Board;
import tractorlk.drops.swing.DropsPanel;
import tractorlk.game.FluidGame;
import tractorlk.game.OngoingFluidGame;

/**
 * @author Adam
 * 
 */
public class FluidCubedApplet extends JApplet {

	private static final long serialVersionUID = -8356000171507539877L;

	final Board engine = new Board(new Dimension(590, 550));

	final FluidGame g = new OngoingFluidGame();

	final DropsPanel p = new DropsPanel(engine);

	final Timer t = new Timer(33, new ActionListener() {

		public void actionPerformed(ActionEvent evt) {
			g.run();
			p.repaint();
		}

	});

	@Override
	public void init() {
		super.init();
		// JFrame f = new JFrame("Fluid Cubed - Copyright 2007, Adam L. Davis");
		setSize(600, 600);

		JPanel mainP = new JPanel(new BorderLayout());
		mainP.add(p);
		getContentPane().add(mainP);

		g.start(engine);

		JButton b = new JButton("Restart");
		JButton btnPause = new JButton("Pause");
		b.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				g.restart(engine);
			}

		});
		btnPause.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				if (t.isRunning())
					t.stop();
				else
					t.start();
			}

		});
		JPanel buttons = new JPanel();
		buttons.add(b);
		buttons.add(btnPause);
		mainP.add(buttons, BorderLayout.SOUTH);

		setVisible(true);
	}

	@Override
	public void start() {
		super.start();
		t.start();
	}

	@Override
	public void stop() {
		super.stop();
		t.stop();
	}

}
